package org.example;

public class Test2 {
    public int[] searchRange(int[] nums, int target) {
        int left = getLeftBorder(nums,target);
        int right = getRightBorder(nums,target);
        if (left == -1 || right == -1) return new int[]{-1,-1};
        else return new int[]{left,right};
    }

    public int getRightBorder(int[] nums,int target) {
        //求右边界的时候将数组分为小于等于和大于target的部分
        int n = nums.length;
        if (n == 0) return -1;
        int l = 0, r = n - 1, mid = 0;
        while (l < r) {
            mid = l + (r - l + 1) / 2;
            if (nums[mid] > target) {
                r = mid - 1;
            }else {
                l = mid;
            }
        }
        return nums[l] == target ? l : -1;
    }

    public int getLeftBorder(int[] nums, int target) {
        //求左边界的时候将数组分为小于target的部分和大于等于target的部分
        int n = nums.length;
        if (n == 0) return -1;
        int l = 0, r = n - 1, mid = 0;
        while (l < r) {
            mid = l + (r - l) / 2;
            if (nums[mid] < target) {
                l = mid + 1;
            }else {
                r = mid;
            }
        }
        return nums[l] == target ? l : -1;
    }

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        int[] arr = {5,7,7,8,8,10};
        test2.searchRange(arr,8);
        System.out.println(666);
    }
}
